//
//  LCWTopView.swift
//  MyWB
//
//  Created by 李成万 on 16/6/27.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit
import SnapKit

class LCWTopView: UIView {

    var selfBottomContraint:Constraint?
    
    var status:LCWStatus?{
        didSet{
           iconImageView.sd_setImageWithURL(NSURL(string: (status?.user?.profile_image_url ?? "")), placeholderImage: UIImage(named: "avatar_default"))
            
            nameLabel.text = status?.user?.screen_name
            
            vipImageView.image = status?.user?.mbrankImage
            
            avatar.image = status?.user?.verifiedImage
            
            originalLabel.attributedText = status?.attrString
            
//            sourceLabel.text = getSource(status?.source ?? "")
            sourceLabel.attributedText = status?.sourceAttr
            
            timeLabel.text = status?.createAt
            
            //卸载
            selfBottomContraint?.uninstall()
            if status?.pic_urls?.count == 0{
                picView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    selfBottomContraint = make.bottom.equalTo(originalLabel).offset(Padding).constraint
                }
            }else{
                picView.pic_urls = status?.pic_urls
                picView.hidden = false
                self.snp_makeConstraints { (make) -> Void in
                    selfBottomContraint = make.bottom.equalTo(picView).offset(Padding).constraint
                }
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //创建私有控件
 /// 头像
    private lazy var iconImageView:UIImageView = UIImageView(imageName: "avatar_default_small")
 /// 昵称
    private lazy var nameLabel: UILabel = UILabel(messageColor: UIColor.blackColor(), textFont: 14)
 /// vip图标
    private lazy var vipImageView : UIImageView = UIImageView(imageName: "common_icon_membership_level6")
 /// 发微博时间
    private lazy var timeLabel : UILabel = UILabel(messageColor: ThemeColor, textFont: 12)
 /// 微博来源
    private lazy var sourceLabel : UILabel = UILabel(messageColor: UIColor.lightGrayColor(), textFont: 12)
    /// 认证信息
    private lazy var avatar : UIImageView = UIImageView(imageName: "avatar_grassroot")
    /// 原创微博内容
    private lazy var originalLabel : UILabel = UILabel(messageColor: UIColor.blackColor() , textFont: 15, maxWidth: SCREENW - 2*Padding)
    private lazy var picView : LCWPicCollection = LCWPicCollection()
    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatar)
        addSubview(originalLabel)
        addSubview(picView)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(Padding)
            make.top.equalTo(self).offset(Padding)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImageView.snp_trailing).offset(Padding)
            make.top.equalTo(self).offset(Padding)
        }
        
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(Padding)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImageView.snp_trailing).offset(Padding)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(Padding)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        avatar.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_right)
            make.centerY.equalTo(iconImageView.snp_bottom)
        }
        
        originalLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(Padding)
            make.leading.equalTo(iconImageView)
        }
        
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalLabel.snp_bottom).offset(Padding)
            make.leading.equalTo(self).offset(Padding)
        }
        self.snp_makeConstraints { (make) -> Void in
           selfBottomContraint = make.bottom.equalTo(picView).offset(Padding).constraint
        }
    
    }
    
    
}
